const Banner = require("../models/Banner");
const {uploadURL, uploadPath} = require('../config')
const fs = require("fs");
const path = require("path");

//新增轮播图
exports.addBanner = async (req, res, next) => {
  try {
    console.log("-> req.body.is_use", typeof req.body.is_use, req.body.is_use);
    const model = await Banner.create({carousel: req.imgUrl, is_use: req.body.is_use})

    res.send({
      code: 20000,
      message: '添加轮播图成功'
    })

  } catch (err) {
    console.log(err)
    next(err)
  }

}

//后台管理系统获取轮播图列表
exports.getBanner = async (req, res, next) => {
  const pageNumber = req.query.pageNumber ? parseInt(req.query.pageNumber) : 1
  const pageSize = req.query.pageSize ? parseInt(req.query.pageSize) : 10
  try {
    let total = await Banner.find().count()
    let result = await Banner.find().sort({
      'is_use': -1,
      date: -1
    }).select("_id carousel is_use date").skip((pageNumber - 1) * pageSize).limit(pageSize)
    result = result.map(item => {
      return {
        _id: item._id,
        carousel: uploadURL + item.carousel,
        date: item.date,
        is_use: item.is_use,
      }
    })
    res.json({
      code: 20000,
      data: result,
      pagination: {
        total,
        pageSize,
        pageNumber
      }
    })
  } catch (err) {
    next(err)
  }
}
// 前台页面轮播图
exports.indexBanners = async (req, res, next) => {
  try {
    let total = await Banner.find({is_use: true}).count()
    let result = await Banner.find({is_use: true}).sort({'is_use': -1, date: -1}).select("_id carousel is_use date")
    result = result.map(item => {
      return {
        _id: item._id,
        carousel: uploadURL + item.carousel
      }
    })
    res.json({
      code: 20000,
      data: result
    })
  } catch (err) {
    next(err)
  }
}
// 删除轮播图
exports.delBanner = async (req, res, next) => {
  const _id = req.params.id
  try {
    let result = await Banner.findByIdAndDelete(_id)
    let imgPath = path.join(uploadPath, `/${result.carousel}`)
    // 如果存在，删除本地图片文件
    if (fs.existsSync(imgPath)) {
      fs.unlinkSync(imgPath)
    }
    res.json({
      code: 20000,
      message: '删除轮播图成功'
    })
  } catch (err) {
    next(err)
  }
}

//修改轮播图
exports.updateBanner = async (req, res, next) => {
  const _id = req.params.id
  const is_use = req.body.is_use
  try {
    let oldResult = await Banner.findById(_id)
    let imgPath = path.join(uploadPath, `/${oldResult.carousel}`)
    // 如果存在，删除本地图片文件
    if (fs.existsSync(imgPath)) {
      fs.unlinkSync(imgPath)
    }
    await Banner.findByIdAndUpdate(_id, {carousel: req.imgUrl, is_use, date: Date.now()})
    res.json({
      code: 20000,
      message: '更新轮播图成功'
    })
  } catch (err) {
    next(err)
  }
}

//改变启用状态
exports.is_useBanner = async (req, res, next) => {
  const _id = req.body.id
  const is_use = req.body.is_use
  var json = {is_use: is_use, date: Date.now()}
  try {
    await Banner.findByIdAndUpdate(_id, json)
    res.json({
      code: 20000,
      message: '更新状态成功'
    })
  } catch (err) {
    next(err)
  }
}
